package com.aso114.project.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.StrictMode;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BasesAdapter;
import com.aso114.project.bean.QuestionBean;

import java.net.URL;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asus on 2018/11/12.
 */

public class CollectAdapter extends BasesAdapter<CollectAdapter.ViewHolder> {

    private OnItemClickListener mListener;

    private ArrayList<QuestionBean.QuestionBaseListBean.questionListBean> questionList = new ArrayList<>();

    public CollectAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(getInflater().inflate(R.layout.mycollect_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.myCollectTitle.setText("(" + questionList.get(position).getRealPaperTitle() + ")");
//        String title = questionList.get(position).getFormatContent().replace("<p>", "");
//        String content = title.replace("</p>", "");
        String content = questionList.get(position).getFormatContent();
        for (int i = 0; i < questionList.get(position).getQuestioncontentList().size(); i++) {
            if (!"".equals(questionList.get(position).getQuestioncontentList().get(i).getSelectValue()))
                content += "<p>" + questionList.get(position).getQuestioncontentList().get(i).getSelectKey() + "    " + questionList.get(position).getQuestioncontentList().get(i).getSelectValue() + "</p>";
        }
        holder.myCollectContent.setText(Html.fromHtml(content, imgGetter, null));
        holder.myCollectItemLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(position);
                }
            }
        });
        holder.myCollectClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClearClick(position);
                }
            }
        });
    }

    Html.ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
            URL url;
            try {
                url = new URL(source);
                drawable = Drawable.createFromStream(url.openStream(), ""); // 获取网路图片
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth() + 50,
                    drawable.getIntrinsicHeight() + 50);
            return drawable;
        }
    };

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public void setData(ArrayList<QuestionBean.QuestionBaseListBean.questionListBean> questionList) {
        this.questionList = questionList;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.my_collect_title)
        TextView myCollectTitle;
        @BindView(R.id.my_collect_clear)
        ImageView myCollectClear;
        @BindView(R.id.my_collect_item_ly)
        LinearLayout myCollectItemLy;
        @BindView(R.id.my_collect_content)
        TextView myCollectContent;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onItemClearClick(int position);
    }
}
